package com.topisv.tms.service;

import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.topisv.tms.entity.SkuOverInfo;
import com.topisv.tms.repository.SkuOverInfoDao;
import com.topisv.tms.service.base.BaseService;

//Spring Bean的标识.
@Component
// 默认将类中的所有public函数纳入事务管理.
@Transactional(readOnly = true)
public class SkuOverInfoService extends BaseService {
	
	@Autowired
	private SkuOverInfoDao soiDao;
	
	@Transactional(propagation=Propagation.REQUIRES_NEW)
	public List<SkuOverInfo> findByEndFlag(boolean endFlag) {
		return soiDao.findByEndFlag(endFlag);
	}
	
	public List<SkuOverInfo> findBy(final String warehouseCode,final String customerCode,final String sku,final String endFlag){
		Sort sort = new Sort(Direction.ASC, "customerCode","warehouseCode","sku", "opeTime", "startTime");
		return soiDao.findAll(new Specification<SkuOverInfo>() {
			@Override
			public Predicate toPredicate(Root<SkuOverInfo> r, CriteriaQuery<?> q, CriteriaBuilder cb) {

				Predicate pre = cb.conjunction();
				
				if (StringUtils.isNotBlank(warehouseCode)) {
					pre.getExpressions().add(cb.equal(r.<String> get("warehouseCode"), warehouseCode));
				}
				if (StringUtils.isNotBlank(customerCode)) {
					pre.getExpressions().add(cb.equal(r.<String> get("customerCode"), customerCode));
				}
				if (StringUtils.isNotBlank(sku)) {
					pre.getExpressions().add(cb.equal(r.<String> get("sku"), sku));
				}
				if (StringUtils.isNotBlank(endFlag)) {
					if("1".equals(endFlag)){
						pre.getExpressions().add(cb.isTrue(r.<Boolean> get("endFlag")));
					}else {
						pre.getExpressions().add(cb.isFalse(r.<Boolean> get("endFlag")));
					}
				}
				return pre;
			}

		},sort);
	}
	
	
	public List<SkuOverInfo> findBy(final String warehouseCode,final String customerCode,final String sku,final Date opeTime,final String endFlag){
		return soiDao.findAll(new Specification<SkuOverInfo>() {
			@Override
			public Predicate toPredicate(Root<SkuOverInfo> r, CriteriaQuery<?> q, CriteriaBuilder cb) {

				Predicate pre = cb.conjunction();
				
				if (StringUtils.isNotBlank(warehouseCode)) {
					pre.getExpressions().add(cb.equal(r.<String> get("warehouseCode"), warehouseCode));
				}
				if (StringUtils.isNotBlank(customerCode)) {
					pre.getExpressions().add(cb.equal(r.<String> get("customerCode"), customerCode));
				}
				if (StringUtils.isNotBlank(sku)) {
					pre.getExpressions().add(cb.equal(r.<String> get("sku"), sku));
				}
				if (StringUtils.isNotBlank(sku)) {
					pre.getExpressions().add(cb.equal(r.<String> get("opeTime"), opeTime));
				}
				if (StringUtils.isNotBlank(endFlag)) {
					if("1".equals(endFlag)){
						pre.getExpressions().add(cb.isTrue(r.<Boolean> get("endFlag")));
					}else {
						pre.getExpressions().add(cb.isFalse(r.<Boolean> get("endFlag")));
					}
				}
				return pre;
			}

		});
	}
	
	public Page<SkuOverInfo> findBy(final String warehouseCode,final String customerCode,final String sku,final String endFlag,int pageNumber,int pageSize){
		Sort sort = new Sort(Direction.ASC, "sku", "opeTime", "startTime");
		PageRequest pageRequest = new PageRequest(pageNumber-1, pageSize,sort);
		
		return soiDao.findAll(new Specification<SkuOverInfo>() {
			@Override
			public Predicate toPredicate(Root<SkuOverInfo> r, CriteriaQuery<?> q, CriteriaBuilder cb) {

				Predicate pre = cb.conjunction();
				
				if (StringUtils.isNotBlank(warehouseCode)) {
					pre.getExpressions().add(cb.equal(r.<String> get("warehouseCode"), warehouseCode));
				}
				if (StringUtils.isNotBlank(customerCode)) {
					pre.getExpressions().add(cb.equal(r.<String> get("customerCode"), customerCode));
				}
				if (StringUtils.isNotBlank(sku)) {
					pre.getExpressions().add(cb.equal(r.<String> get("sku"), sku));
				}
				if (StringUtils.isNotBlank(endFlag)) {
					if("1".equals(endFlag)){
						pre.getExpressions().add(cb.isTrue(r.<Boolean> get("endFlag")));
					}else {
						pre.getExpressions().add(cb.isFalse(r.<Boolean> get("endFlag")));
					}
				}

				return pre;
			}

		},pageRequest);
	}
	
	@Transactional(readOnly=false,propagation=Propagation.REQUIRES_NEW)
	public void save(List<SkuOverInfo> soiList){
		soiDao.save(soiList);
	}
	
	@Transactional(readOnly=false,propagation=Propagation.REQUIRES_NEW)
	public void delete(List<SkuOverInfo> soiList){
		soiDao.delete(soiList);
	}
}
